package net.wanho.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import net.wanho.client.UserClient;
import net.wanho.entity.User;
import net.wanho.util.BlockExceptionUtil;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Author：汤小洋
 * Date：2024-10-30 16:06
 * Description：<描述>
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    // private RestTemplate restTemplate;
    private UserClient userClient;

    @SentinelResource(value = "MyTestResource", blockHandlerClass = BlockExceptionUtil.class, blockHandler = "handleException")
    @GetMapping("/test")
    public AjaxResult test(){
        // String str = restTemplate.getForObject("http://localhost:6001/users/test", String.class);
        String str = userClient.test();
        return AjaxResult.success("consumer------>" + str);
    }

    @GetMapping
    public AjaxResult list(String username){
         return userClient.list(username);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable int id){
        return userClient.remove(id);
    }


    @PostMapping
    public AjaxResult add(@RequestBody User user){
        return userClient.add(user);
    }

}
